import type {
  AddReq,
  CreateCrudOptionsRet,
  DelReq,
  EditReq,
  UserPageQuery,
  ValueBuilderContext,
} from '@fast-crud/fast-crud';

import { dict } from '@fast-crud/fast-crud';
import dayjs from 'dayjs';

import * as api from './api';

export default function crud(
): CreateCrudOptionsRet {
  return {
    crudOptions: {
      request: {
        addRequest: async ({ form }: AddReq) => await api.AddObj(form),
        delRequest: async ({ row }: DelReq) => await api.DelObj(row.id),
        editRequest: async ({ form }: EditReq) => await api.UpdateObj(form),
        pageRequest: async (query: UserPageQuery) => await api.GetPage(query),
      },
      table: { rowKey: 'clientId' },
      toolbar: {},
      columns: {
        id: {
          title: 'ID',
          type: 'text',
          form: { show: false },
          column: { show: false },
        },
        clientId: {
          title: '客户标识',
          type: 'text',
          search: { show: true },
          column: { ellipsis: true, width: 140 },
          editForm: { component: { disabled: true } },
          form: {
            rules: [{ required: true, message: 'clientId 不能为空' }],
          },
        },
        clientName: {
          title: '客户名称',
          type: 'text',
          column: { ellipsis: true, width: 180 },
          search: { show: true },
          form: {
            rules: [{ required: true, message: '客户名称不能为空' }],
          },
        },
        clientSecret: {
          title: '客户秘钥',
          type: 'text',
          column: { width: 140, show: true },
          // editForm: { show: false },
          form: {
            rules: [{ required: true, message: 'clientSecret 不能为空' }],
            // helper: '注意：填写后将以密文方式存储且不允许修改',
          },
        },
        status: {
          title: '状态',
          type: 'dict-radio',
          column: { show: true, width: 80 },
          search: { show: true },
          addForm: { value: 1 },
          dict: dict({
            data: [
              { value: 1, label: '启用', color: 'success' },
              { value: 0, label: '禁用', color: 'error' },
            ],
          }),
          valueBuilder({ value, row, key }: ValueBuilderContext): void {
            if (value !== null) {
              row[key] = value === true ? 1 : 0;
            }
          },
        },
        authorizationGrantTypes: {
          title: '授权类型',
          type: 'dict-select',
          form: {
            rules: [{ required: true, message: '授权范围不能为空' }],
            component: { mode: 'multiple' },
          },
          dict: dict({
            data: [
              { value: 'password', label: '密码模式' },
              { value: 'sms', label: '短信登录' },
              { value: 'email', label: '邮箱模式' },
              { value: 'vc_code', label: '验证码模式' },
            ],
          }),
          column: { width: 200, component: { color: 'auto' } },
        },
        scopes: {
          title: '授权范围',
          type: 'dict-select',
          form: {
            rules: [{ required: true, message: '授权范围不能为空' }],
            component: { mode: 'multiple' },
          },
          dict: dict({
            data: [
              { value: 'read', label: '只读' },
              { value: 'write', label: '可写' },
              { value: 'openid', label: 'OpenId' },
              { value: 'mobile', label: '手机号' },
              { value: 'profile', label: '用户配置' },
              { value: 'mini_app', label: '小程序' },
              { value: 'server', label: '服务端直连' },
              { value: 'client', label: '客户端模式' },
              { value: 'tripartite', label: '服务端直连' },
              { value: 'platform', label: 'platform' },
              { value: 'web', label: 'web' },
              { value: 'message.read', label: 'message.read' },
              { value: 'message.write', label: 'message.write' },
            ],
          }),
          column: { width: 200, component: { color: 'auto' } },
        },
        clientIdIssuedAt: {
          title: '生效时间',
          column: { width: 200 },
          type: 'datetime',
          valueBuilder({ value, row, key }: ValueBuilderContext): void {
            if (value !== null) {
              row[key] = dayjs(value);
            }
          },
          valueResolve({ value, row, key }: ValueBuilderContext): void {
            if (value !== null) {
              row[key] = dayjs(value).unix();
            }
          },
          form: {
            helper: '不填则默认当前时间',
          },
        },
        clientSecretExpiresAt: {
          title: '过期时间',
          column: { width: 200 },
          type: 'datetime',
          valueBuilder({ value, row, key }: ValueBuilderContext): void {
            if (value !== null) {
              row[key] = dayjs(value);
            }
          },
          valueResolve({ value, row, key }: ValueBuilderContext): void {
            if (value !== null) {
              row[key] = dayjs(value).unix();
            }
          },
          form: {
            helper: '不填则永不失效',
          },
        },
        accessTokenTimeToLive: {
          title: 'Access Token 有效期',
          type: 'text',
          column: { ellipsis: true, width: 180 },
          addForm: {
            value: '120',
          },
          form: {
            component: {
              addonAfter: '分钟',
            },
            rules: [{ required: true, message: 'Access Token有效期不能为空' }],
            helper: 'Access Token有效期,默认2小时',
          },
        },
        refreshTokenTimeToLive: {
          title: 'Refresh Token 有效时长',
          type: 'text',
          column: { ellipsis: true, width: 180, show: true },
          addForm: {
            value: '4320',
          },
          form: {
            component: {
              addonAfter: '分钟',
            },
            rules: [{ required: true, message: 'Token有效期不能为空' }],
            helper: 'Refresh Token有效期,默认3天(4320)',
          },
        },
        deviceCodeTimeToLive: {
          title: '设备码 有效时长（分钟）',
          type: 'text',
          column: { ellipsis: true, width: 180, show: true },
          addForm: {
            value: '120',
          },
          form: {
            component: {
              addonAfter: '分钟',
            },
            rules: [{ required: true, message: '设备码有效期不能为空' }],
            helper: '设备码有效期,默认120分钟',
          },
        },
        redirectUris: {
          title: '回调地址',
          type: 'textarea',
          column: { ellipsis: true, show: false },
          form: {
            rules: [{ required: true, message: '请填写回调地址' }],
            col: {
              span: 24,
            },
          },
        },
      },
    },
  };
}
